package com.yc.jpyy.mobile.controller;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.TeacherDao;
import com.yc.jpyy.mobile.service.TeacherService;

@Controller
@RequestMapping(value = "/teacherclass")
public class TeacherClassInfo extends BaseController{
	@Autowired
	private TeacherService teacherService;
	private static Logger logger = LoggerFactory.getLogger(TeacherClassInfo.class);	
	@Autowired
	private TeacherDao teacherDao;
	
	
	/**
	 * 根据教练ID获取七天时间列表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getCourseCountByTecID", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getCourseCountByTecID(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练ID
			String teacherId = request.getParameter("teacherId");
		 
			if(teacherId != null && !teacherId.isEmpty())
			{ 
				List<Map<String, Object>> stumap = teacherService.getCourseCountByTecID(teacherId);
				if (stumap != null && stumap.size() > 0) {
					SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd");
					DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
					List<Map<String, Object>> resultmap = new ArrayList<Map<String,Object>>();
					List<LocalDateTime> sevenDay = getSevenbDay();
					sevenDay.stream().forEach(time -> {
						long s=	stumap.stream().filter(map->(map.get("courseDate")).toString().substring(0,10)
								.equals(time.format(format))).collect(Collectors.toList()).size();
						if(s>0){
							resultmap.add(stumap.stream().filter(
									map->map.get("courseDate").toString().substring(0,10)
									.equals(time.format(format)))
									.collect(Collectors.toList()).get(0));
						}else{
							Map<String,Object> maps = new HashMap<>();
							maps.put("counts",0);
							try {
								maps.put("courseDate",sdf.parse(time.format(format)));
							} catch (Exception e) {
								e.printStackTrace();
							}
							maps.put("weekdays",getWeekDay(time));
							resultmap.add(maps);
						}
					});
					response.setData(resultmap);
				} else {
					response.setErrorcode(1);
					response.setMessage("该教练下无课程信息...");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("教练ID不能为空...");
			}
		} catch (Exception e) {
			logger.info("getCourseCountByTecID:"+ e.toString());
			response.setReturnMsg(1,"获取失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 根据教练ID获取一天课程列表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getCourseListByTecID", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getCourseListByTecID(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练ID
			String teacherId = request.getParameter("teacherId");
		    //课程日期
			String courseDate = request.getParameter("courseDate");
			if(teacherId != null && !teacherId.isEmpty()
					&& courseDate != null && !courseDate.isEmpty()){ 
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("teacherId",teacherId);
				paramsMap.put("courseDate",courseDate);
				//获取课程信息
				response = teacherService.getCourseListByTecID(paramsMap,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("getCourseListByTecID:"+ e.toString());
			response.setReturnMsg(1,"获取失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 根据课程ID获取学员预约信息
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getAppointmentDetailInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getAppointmentDetailByCourseID(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 课程ID
			String courseID = request.getParameter("courseID");
		 	 
			if(courseID!=null&&!courseID.isEmpty()){	 
				List<Map<String, Object>> stumap = teacherService.getAppointmentDetailByCourseID(courseID,Config.getImageAddress());
				if (stumap != null && stumap.size() > 0) {	 
					// 设置Json集合
					response.setData(stumap);
				} else {
					response.setErrorcode(1);
					response.setMessage("该课程下无预约信息...");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("课程ID不能为空...");
			}
		} catch (Exception e) {
			logger.info("getAppointmentDetailByCourseID:"+ e.toString());
			response.setReturnMsg(1,"获取失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 缺勤标记
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateAppointmentDetailAbsent", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateAppointmentDetailAbsent(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 预约ID
			String appointId = request.getParameter("appointId");
			// 学员ID
			String studentId = request.getParameter("studentId");
			if(appointId != null && !studentId.isEmpty()
					&& studentId != null && !appointId.isEmpty()){ 
				response = teacherService.updateAppointmentDetailAbsent(appointId,studentId,response);
			} else {
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}

		} catch (Exception e) {
			logger.info("updateAppointmentDetailAbsent:"+ e.toString());
			response.setReturnMsg(1,"修改失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	/**
	 * 历史课程查询
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getOldTeacherList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getOldTeacherList(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练编号
			String teacherId = request.getParameter("teacherId");
			// 月份
			String monthInfo = request.getParameter("monthInfo");
			
			Map<String, Object> valMap = new HashMap<String, Object>(); 
			if(teacherId != null && !teacherId.isEmpty() 
					&& monthInfo != null && !monthInfo.isEmpty()){
				valMap.put("teacherId", teacherId);
				valMap.put("monthInfo", monthInfo);
				List<Map<String, Object>> stumap = teacherService.getOldTeacherList(valMap);
				if(null != stumap && stumap.size() > 0){
					response.setData(stumap);
				} else {
					response.setErrorcode(1);
					response.setMessage("该月份无数据");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("教练编号或月份不能为空...");
			}
		} catch (Exception e) {
			logger.info("getOldTeacherList:"+ e.toString());
			response.setReturnMsg(1,"操作失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	
	
	/**
	 * 教课教练学员查询接口
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTeacherStuListByTecID", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTeacherStuListByTecID(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 教练ID
			String teacherid = request.getParameter("teacherid");
		  
			if(teacherid != null && !teacherid.isEmpty()){
				List<Map<String, Object>> stumap = teacherService.getTeacherStuListByTecID(teacherid);
				if (stumap != null && stumap.size() > 0) {	 
					// 设置Json集合
					response.setData(stumap);
				} else {
					response.setErrorcode(1);
					response.setMessage("该教练无课程信息...");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("教练ID不能为空...");
			}
		} catch (Exception e) {
			logger.info("getTeacherStuListByTecID:"+ e.toString());
			response.setReturnMsg(1,"获取失败！");
		} finally {
			return response.toJSON();
		}
	}
}
